﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.BusinessImpl;
using Wicresoft.UnifyShow2.Portal.Models;
using Wicresoft.UnifyShow2.Portal.Service;

namespace Wicresoft.UnifyShow2.Portal.Controllers
{
    [Authorize]
    public class NetworkController : BaseController
    {
        private IUser_X user_x;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)user_x).Dispose();
        }

        [InjectionConstructor]
        public NetworkController(IUser_X _user_x)
        {
            user_x = _user_x;
        }


        //
        // GET: /Network/
        public ActionResult Index()
        {
            SetRoles(user_x);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user_x.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            var sites = user_x.GetSiteBranchByID(USER_X.ID);
            var company = USER_X.Company;
            if (USER_X.Department == "省局机关运维")
            {

                ISiteBranch sitebranchs = new SiteBranch();
                sites.Clear();
                sites.Add(sitebranchs.GetSiteALLBranch().FirstOrDefault(s => s.Name == USER_X.Department));
                company = USER_X.Department;
            }

            var mysites = new List<SiteBranchModel>();
            foreach (var item in sites)
            {
                if (item != null)
                {
                    if (!string.IsNullOrEmpty(item.Name))
                        mysites.Add(item);
                }
            }


            if (mysites.Count() > 0)
            {
                var urlmodel = new SiteBranchModel();
                if (mysites.Count() == 1 && company == "省局机关运维")
                    urlmodel = mysites.FirstOrDefault();
                else
                    urlmodel = mysites.FirstOrDefault(s => s.Name == company);

                if (urlmodel != null)
                    ViewBag.Url = urlmodel.ET_IPPort2;
                else
                    ViewBag.Url = string.Empty;
            }
            else
            {
                ViewBag.Url = string.Empty;
            }

            return View();
        }


        public ActionResult towz4(string Url)
        {
            NetworkHelp netHelp = new NetworkHelp();
            var USER_X = user_x.GetUser_XByName(User.Identity.Name);
            var sites = user_x.GetSiteBranchByID(USER_X.ID);

            var company = USER_X.Company;
            if (USER_X.Department == "省局机关运维")
            {
                ISiteBranch sitebranchs = new SiteBranch();
                sites.Add(sitebranchs.GetSiteALLBranch().FirstOrDefault(s => s.Name == USER_X.Department));
                company = USER_X.Department;
            }

            var mysites = new List<SiteBranchModel>();
            foreach (var item in sites)
            {
                if (item != null)
                {
                    if (!string.IsNullOrEmpty(item.Name))
                        mysites.Add(item);
                }
            }

            if (mysites.Count() > 0)
            {

                if (mysites.Count() == 1 && company == "省局机关运维")
                {
                    ViewBag.Url = mysites.FirstOrDefault().ET_IPPort + "?UserName=admin&Password=admin&callingUrl=/webUI/main.do";
                }
                else if (company == "福建烟草海晟投资管理有限公司") 
                {
                    ViewBag.Url = mysites.FirstOrDefault(s => s.Name == company).ET_IPPort + "?UserName=haitou&Password=haitou&callingUrl=/webUI/main.do";
                }
                else if (company == "中国烟草福建进出口公司")
                {
                    ViewBag.Url = mysites.FirstOrDefault(s => s.Name == company).ET_IPPort + "?UserName=jinchukou&Password=jinchukou&callingUrl=/webUI/main.do";
                }
                else
                {
                    ViewBag.Url = mysites.FirstOrDefault(s => s.Name == company).ET_IPPort + "?UserName=admin&Password=admin&callingUrl=/webUI/main.do";
                    //var mysite = mysites.FirstOrDefault(s => s.Name == company);
                    //int myid = Convert.ToInt32(mysite.ID);
                    //var account = netHelp.getAccount(myid, mysite.Name);
                    //ViewBag.Url = mysites.FirstOrDefault(s => s.Name == company).ET_IPPort + "?UserName=" + account.name + "&Password=" + account.pwd + "&callingUrl=/webUI/main.do";
                }
            }
            else
            {
                ViewBag.Url = string.Empty;
            }
            return View();

        }


    }
}
